package com.ghck.shop.utils;

import java.io.Serializable;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

public class StringUtil implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static boolean checkNotThreadIdIfOK(String threadId){
		boolean flag = true;
		if(StringUtils.isEmpty(threadId)){
			return false;
		}
		try {
			String[] strs = threadId.split("_");
			if(strs.length!=2){
				return false;
			}
			if(Integer.parseInt(strs[0])==0){
				return false;
			}
			if(Integer.parseInt(strs[1])==0){
				return false;
			}
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	/**
	 * 生成订单号 格式:yyyyMMddHHmmss+四位随机数字 总长18位
	 * @return
	 */
	public static String generateOrderNO(){
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append(DateUtils.getCurrentTime2StringBySec());
		sBuffer.append(StringUtil.getRandomNumberByLength(4));
		return sBuffer.toString();
	}
	
	/**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomNumberByLength(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
